#pragma	once

#include "ZOdbcPrerequisite.h"
#include <string>
#include <sql.h>
#include <sqlext.h>
#include <boost/shared_ptr.hpp>

namespace	Z	{

class	FrameAllocator;

namespace	Odbc	{

//------------------------------------------------------------------------------------------------------------------------
class	_ZOdbcApi	Connection
{
private:
	struct	Provider{
		Provider(const std::string& kServerName, const std::string& kUserName, const std::string& kAuthentication);
		std::string	serverName; 
		std::string	userName;
		std::string	authentication;
	};
public:
	Connection(const std::string& serverName, const std::string& userName, const std::string& authentication);
	~Connection();
	bool	open();
	void	close();
	bool	beginTrans();
	bool	commitTrans();
	bool	rollbackTrans();
	RecordsetPtr	execute(Command& kCommand);
	RecordsetPtr	execute(const char* format, ...);
	SQLHDBC	dbc()	const	{	return mDbc;	}
	void*	allocateUnaligned(size_t size);
	void*	allocateAligned(size_t size, size_t alignment);
	void	resetAllocator();
private:
	bool	setAutoCommitOff();
	bool	transact(SQLUSMALLINT completionType);

private:
	SQLHENV	mEnv;
	SQLHDBC	mDbc;
	Provider	mProvider;
	bool	mOpened;
	bool	mTransaction;
	typedef	FrameAllocator	Allocator;
	Allocator*	mAllocator;
};

_ZOdbcApi	extern	bool		setConnectionPooling();
extern	void	diagnosis(SQLSMALLINT fHandleType, SQLHANDLE handle, const std::string& cause = "");

}

}